package com.fanshuai2.niuke.search.dfs;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Migong2 {
    static class Position {
        int x;
        int y;
        public Position(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public String toString() {
            return "(" + x + "," + y + ")";
        }
    }

    static List<Position> getPath(int[][] migong) {
        List<Position> list = new ArrayList<>();
        boolean[][] visited = new boolean[migong.length][migong[0].length];

        dfs(migong, visited, 0, 0, list);
        return list;
    }

    static boolean dfs(int[][] migong, boolean[][] visited,
                       int x, int y, List<Position> path) {
        path.add(new Position(x, y));
        visited[x][y] = true;

        int m = migong.length;
        int n = migong[0].length;
        if (x == m - 1 && y == n - 1) {
            return true;
        }

        if (x + 1 <= m - 1 && !visited[x + 1][y] && migong[x + 1][y] == 0) {
            if (dfs(migong, visited, x + 1, y, path)) {
                return true;
            }
        }
        if (x - 1 >= 0 && !visited[x - 1][y] && migong[x - 1][y] == 0) {
            if (dfs(migong, visited, x - 1, y, path)) {
                return true;
            }
        }
        if (y + 1 <= n - 1 && !visited[x][y + 1] && migong[x][y + 1] == 0) {
            if (dfs(migong, visited, x, y + 1, path)) {
                return true;
            }
        }
        if (y - 1 >= 0 && !visited[x][y - 1] && migong[x][y - 1] == 0) {
            if (dfs(migong, visited, x, y - 1, path)) {
                return true;
            }
        }

        visited[x][y] = false;
        path.remove(path.size() - 1);
        return false;
    }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNextInt()) {
            int m = in.nextInt();
            int n = in.nextInt();
            int[][] migong = new int[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    migong[i][j] = in.nextInt();
                }
            }
            List<Position> fullpath = getPath(migong);
            for (Position pos : fullpath) {
                System.out.println(pos.toString());
            }
        }
    }
}
